<template>
  <div :id="id" :class="className" 
  
  :numMale="numMale" 
  :numFemale="numFemale"

  :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'

export default {
  name: 'Pie2',
  mixins: [resize],
  props: {
    numFemale: {
      type: Number,
      default: 1
    },
    numMale : {
      type: Number,
      default: 1
    },

    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null,
      series: [
        { value: 1, name: 'numFemale' },
        { value: 2, name: 'numMale' },
      ],
    }
  },  
  // 侦听组件实例上的响应式 property 或函数计算结果的变化
  watch: {
    numFemale(val) {
      this.series[0].value = val;
      this.initChart();
    },
    
    deep: true,
  },
  computed: {
  },
  methods: {
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },

  methods: {
    initChart() {

      this.series[0].value=this.numFemale;
      this.series[1].value=this.numMale;

      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        title: {
          text: '某站点用户访问来源',
          subtext: '纯属虚构-------------',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: '50%',
            data: this.series,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    },

  }
}
</script>
